const Koa = require('koa')
const Router = require('koa-router')
const app = new Koa()
const router = new Router()

const { Wechat } = require('wechat-jssdk')
const wx = new Wechat({
  // 配置自己的测试号的APPID和APPsecret
  "appId": "wxe733093faa04b8aa",
  "appSecret": "24f11b6815515016170c4ffeb6977e65",
})


// 定义API接口
router.get('/news/list', (ctx) => {
  ctx.body = {
    status: 200,
    data: [{ id: 1, name: 123 }]
  }
}).get('/api/sign', async (ctx) => {
  const signUrl = ctx.request.query.signUrl || 'http://localhost:8080/'
  const sign = await wx.jssdk.getSignature(signUrl)
  ctx.body = { status: 200, data: sign }
})

// 注册
app.use(router.routes()).use(ctx => {
  ctx.body = '<h1>Hello Koa</h1>'
})

app.listen(3000, () => console.log(`服务器运行在：http://localhost:3000`))